import os
import json

from AtomClicker.app.common.StepModel import StepModel
from AtomClicker.config.config import SCRIPTS_DIR


def execute_user_script(script_path):
    atomic_list = []
    exist_script = []
    def func(sp):
        if sp in exist_script:
            raise Exception("不支持循环调用脚本")
        with open(os.path.join(SCRIPTS_DIR, f"{sp}.json"), 'r', encoding='utf8') as f:
            user_script = json.load(f)
            exist_script.append(sp)
        for _, v in user_script.items():
            step = StepModel(**v)
            if step.action == "workflow":
                func(step.content)
                continue
            atomic_list.append({
                "action": step.action,
                "params": step.content
            })
    func(script_path)
    return atomic_list

